package com.sh.rbac.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sh.rbac.core.common.annotation.SysLog;
import com.sh.rbac.core.common.enums.OperationTypeEnum;
import com.sh.rbac.core.common.result.Result;
import com.sh.rbac.domain.dto.dict.item.DictItemDeleteDTO;
import com.sh.rbac.domain.dto.dict.item.DictItemQueryDTO;
import com.sh.rbac.domain.dto.dict.item.DictItemSaveDTO;
import com.sh.rbac.domain.dto.dict.item.DictItemUpdateDTO;
import com.sh.rbac.domain.entity.DictItem;
import com.sh.rbac.service.DictItemService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 字典子项控制器
 *
 * @author wb
 * @since 2024-06-28
 */
@Tag(name = "字典子项管理", description = "字典子项相关操作")
@RequestMapping("sys/dict-item")
@RestController
public class DictItemController {

    @Autowired
    private DictItemService dictItemService;


    @Operation(summary = "分页查询字典子项列表", description = "根据字典子项条件查询参数进行查询")
    @ApiResponse(responseCode = "200", description = "返回查询结果", content = @Content(schema = @Schema(implementation = DictItem.class)))
    @PostMapping("page/{current}/{limit}")
    @PreAuthorize("@ps.hasPerm('sys:dict-item:page')")
    public Result<Page<DictItem>> getPage(
            @Parameter(description = "当前页码", required = true, example = "1") @PathVariable Long current,
            @Parameter(description = "每页记录数", required = true, example = "10") @PathVariable Long limit,
            @RequestBody DictItemQueryDTO dictItemQueryDTO) {
        return Result.success(dictItemService.getPage(current, limit, dictItemQueryDTO));
    }


    @SysLog(module = "系统管理->字典子项管理", type = OperationTypeEnum.ADD)
    @Operation(summary = "添加字典子项", description = "保存一个新字典子项")
    @ApiResponse(responseCode = "200", description = "成功保存字典子项")
    @PostMapping("save")
    @PreAuthorize("@ps.hasPerm('sys:dict-item:save')")
    public Result<?> save(@RequestBody DictItemSaveDTO dictItemSaveDTO) {
        return dictItemService.save(dictItemSaveDTO) ? Result.success() : Result.failure();
    }


    @SysLog(module = "系统管理->字典子项管理", type = OperationTypeEnum.UPDATE)
    @Operation(summary = "更新字典子项", description = "根据ID更新字典子项信息")
    @ApiResponse(responseCode = "200", description = "成功更新字典子项")
    @PutMapping("update")
    @PreAuthorize("@ps.hasPerm('sys:dict-item:update')")
    public Result<?> update(@RequestBody DictItemUpdateDTO dictItemUpdateDTO) {
        return dictItemService.updateById(dictItemUpdateDTO) ? Result.success() : Result.failure();
    }


    @Operation(summary = "根据ID查询字典子项")
    @ApiResponse(responseCode = "200", description = "返回查询结果", content = @Content(schema = @Schema(implementation = DictItem.class)))
    @GetMapping("get/{id}")
    @PreAuthorize("@ps.hasPerm('sys:dict-item:get')")
    public Result<?> get(@PathVariable Long id) {
        return Result.success(dictItemService.getById(id));
    }


    @SysLog(module = "系统管理->字典子项管理", type = OperationTypeEnum.DELETE)
    @Operation(summary = "删除字典子项", description = "删除字典子项信息")
    @ApiResponse(responseCode = "200", description = "成功字典子项菜单")
    @DeleteMapping("delete")
    @PreAuthorize("@ps.hasPerm('sys:dict-item:delete')")
    public Result<?> deleteDictItem(@RequestBody DictItemDeleteDTO dictItemDeleteDTO) {
        return dictItemService.removeDictItem(dictItemDeleteDTO) ? Result.success() : Result.failure();
    }

    @Operation(summary = "根据字典编码查询字典子项")
    @ApiResponse(responseCode = "200", description = "返回查询结果", content = @Content(schema = @Schema(implementation = DictItem.class)))
    @GetMapping("getByDictCode/{dictCode}")
    @PreAuthorize("@ps.hasPerm('sys:dict-item:getByDictCode')")
    public Result<?> getByDictCode(@PathVariable String dictCode) {
        return Result.success(dictItemService.getListByDictCode(dictCode));
    }

    @SysLog(module = "系统管理->字典子项管理", type = OperationTypeEnum.UPDATE)
    @Operation(summary = "更新字典子项状态", description = "根据ID更新字典子项状态")
    @ApiResponse(responseCode = "200", description = "成功更新字典子项状态")
    @PutMapping("update-status/{id}/{status}")
    @PreAuthorize("@ps.hasPerm('sys:dict-item:update-status')")
    public Result<?> updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        return dictItemService.updateStatus(id, status) ? Result.success() : Result.failure();
    }
}
